<?php
function curlPost($url, $postData = array()) {
    $timeout = 30;
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)");
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
    $content = curl_exec($ch);
    curl_close($ch);
    return $content;
}
//对统计数据中空白的日期填上数量0
function fillBlankHour($data, $start_time, $end_time) {
    $data = key2index($data, 'day_h');
    $newData = array();
    for ($t = $end_time; $t >= $start_time; $t -= 3600) {
        $day_h = date('H', $t);
        if (empty($data[$day_h])) {
            $newData[] = array('day_h' => $day_h, 'num' => 0);
        } else {
            $newData[] = $data[$day_h];
        }
    }
    return $newData;
}
function fillBlankDate($data, $start_time, $end_time) {
    $data = key2index($data, 'ymd');
    $newData = array();
    for ($t = $end_time; $t >= $start_time; $t -= 86400) {
        $ymd = date('Ymd', $t);
        if (empty($data[$ymd])) {
            $newData[] = array('ymd' => $ymd, 'num' => 0);
        } else {
            $newData[] = $data[$ymd];
        }
    }
    return $newData;
}

if (!function_exists('getallheaders')) {
    function getallheaders()
    {
        $headers = '';
        foreach ($_SERVER as $name => $value) {
            if (substr($name, 0, 5) == 'HTTP_') {
                $headers[str_replace(' ', '-', ucwords(strtolower(str_replace('_', ' ', substr($name, 5)))))] = $value;
            }
        }
        return $headers;
    }
}

// int转字符串
function num_encode($id)
{
    if ($id > 100) {
        $id = (string)$id;
        $len = strlen($id) - 1;
        $id = $id{0} . $id{$len} . substr($id, 2, -1) . $id{1};
    }
    $id += 60512868;
    $str = base_convert($id, 10, 36);
    $l = strlen($str);
    $str = substr($str, 0, $l - 2) . num_xchange(substr($str, -2, 1)) . num_xchange(substr($str, -1));
    return $str;
}

// 字符串转int
function num_decode($str)
{
    $l = strlen($str);
    $str = substr($str, 0, $l - 2) . num_xchange(substr($str, -2, 1), 1) . num_xchange(substr($str, -1), 1);
    $id = base_convert($str, 36, 10);
    $id -= 60512868;
    if ($id > 100) {
        $id = (string)$id;
        $len = strlen($id) - 1;
        return $id{0} . $id{$len} . substr($id, 2, -1) . $id{1};
    } else {
        return $id;
    }
}

//字符串int转换所需函数
function num_xchange($s, $decode = 0)
{
    if ($decode) {
        $str = "ytuvsrqzxwilng7fed2cbajk1096h53m8o4p";
    } else {
        $str = "poiuytrewqlkjhgfdsamnbvcxz6541239807";
    }
    $s = base_convert($s, 36, 10);
    return $str{$s};
}

function httpThumb($path, $width = 0, $height = 0)
{
    $str = "/admin/public/thumb" . ($width ? "/width/$width" : "");
    $str = $str . ($height ? "/height/$height" : "");
    $str = $str . "?image=$path";
    return $str;
}

/**
 * 发送邮件的函数
 * @param string $to 收件人地址
 * @param string $subject 标题
 * @param string $body 邮件内容
 */
function sendMail($to, $subject, $body)
{
    include_once APP_PATH . 'Common/Util/PHPMailer/class.phpmailer.php';

    $mail = new \PHPMailer();
    $mail->CharSet = "utf-8";
    $mail->IsSMTP(); // telling the class to use SMTP
    $mail->SMTPDebug = false; // enables SMTP debug information (for testing) 1 = errors and messages;2 = messages only
    $mail->SMTPAuth = true; // enable SMTP authentication
    $mail->SMTPSecure = "ssl"; // sets the prefix to the servier
    $mail->Host = "smtp.exmail.qq.com"; // sets QQ as the SMTP server
    $mail->Port = 465; // set the SMTP port for the GMAIL server
    $rand = rand(1, 60);
    $mail->Username = "service{$rand}@manyule.com"; //username
    $mail->Password = "9PqXtiP2MHLEBnBk"; //password
    $mail->SetFrom($mail->Username, "好漫画");

    if (is_array($to)) {
        foreach ($to as $val) {
            $mail->AddAddress($val);
        }
    } else {
        $mail->AddAddress($to);
    }
    $mail->Subject = $subject;
    $mail->MsgHTML($body);

    if (!$mail->Send()) {
        return false;
    } else {
        return true;
    }
}

function getRatableImg($img, $maxWidth, $type = 'jpg')
{
    preg_match("/\.(\d+)x(\d+)\./", $img, $match);
    if (!empty($match[2])) {
        $maxHeight = 99999;
        $scale = min($maxWidth / $match[1], $maxHeight / $match[2]);
        $width = round($match[1] * $scale);
        $height = round($match[2] * $scale);
        $img .= ".{$width}x{$height}.{$type}";
    }
    return $img;
}

/**
 * 指定数组中的某个键值为索引
 */
function key2index($pieces, $key)
{
    $arr = array();
    foreach ($pieces as $v) {
        $arr[$v[$key]] = $v;
    }
    return $arr;
}

// PHP < 5.5.0  copied from php.net by @
if (!function_exists('array_column')) {
    function array_column($input, $column_key, $index_key = null)
    {
        if ($index_key !== null) {
            // Collect the keys
            $keys = array();
            $i = 0; // Counter for numerical keys when key does not exist

            foreach ($input as $row) {
                if (array_key_exists($index_key, $row)) {
                    // Update counter for numerical keys
                    if (is_numeric($row[$index_key]) || is_bool($row[$index_key])) {
                        $i = max($i, (int)$row[$index_key] + 1);
                    }

                    // Get the key from a single column of the array
                    $keys[] = $row[$index_key];
                } else {
                    // The key does not exist, use numerical indexing
                    $keys[] = $i++;
                }
            }
        }

        if ($column_key !== null) {
            // Collect the values
            $values = array();
            $i = 0; // Counter for removing keys

            foreach ($input as $row) {
                if (array_key_exists($column_key, $row)) {
                    // Get the values from a single column of the input array
                    $values[] = $row[$column_key];
                    $i++;
                } elseif (isset($keys)) {
                    // Values does not exist, also drop the key for it
                    array_splice($keys, $i, 1);
                }
            }
        } else {
            // Get the full arrays
            $values = array_values($input);
        }

        if ($index_key !== null) {
            return array_combine($keys, $values);
        }
        return $values;
    }
}